// pages/instdetail/index.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		showDialog: true,
		teacherId: 0,
		showauth: false, //隐藏授权
		showIcon: true, //显示导航图标
		tabactive: 0, //点击第几个tab
		alldetail: [], //教师详情likeFn
		alldetailArry: [], //教师详情
		haslike: false, //关注
		comments: [], //评论
		imgUrls: [],
		sharetip: false, //分享弹框
	},
	

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		var that = this;
		var loginInfo = loginFn()
		loginInfo.teacherId = options.teacherId;
		loginInfo.schoolId = options.schoolId;
		loginInfo.isAdmin = 1;
		that.setData({
			teacherId: options.teacherId,
			schoolId: options.schoolId
		})

		this.getTeacherInfo();
		//获取评论 
		this.getconmentFn(options.teacherId);

	},

	getTeacherInfo() {
		let that = this;
		app.request({
			url: 'teacher/detail',
			data:{
				teacher_id: that.data.teacherId,
				school_id: that.data.schoolId
			},
			sCallback: (res) => {
				if(res.code == 1) {
					let imgUrls = [res.data.uploadImg];
					if(res.data.teacherImages) {
						res.data.teacherImages.forEach((item) => {
							imgUrls.push(item.img)
						})
					}
					that.setData({
						alldetail: res.data,
						stateNum: res.data.schoolTeacherStatus,
						imgUrls: imgUrls
					})
				} else {
					app.alert(res.msg, () => {
						wx.navigateBack();
					})
				}
			}
		})
	},
	//评论列表
	getconmentFn: function(schoolId) {
		var that = this;
		var loginInfo = loginFn()
		if (schoolId) {
			loginInfo.schoolId = schoolId;
		} else {
			loginInfo.schoolId = this.data.alldetail.schoolId;
		}
		wx.request({
			url: urls.listSchool,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					that.setData({
						comments: res.data.data.list
					})
				};
			}
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {
	},
	// 函数
	tabFn: function(e) { //tab点击
		this.setData({
			tabactive: e.currentTarget.id
		})
	},
	changeStatus(){
		let stateNum = this.data.stateNum;
		let status = 3;
		let tips = '确认把该老师设为在职吗？';
		if(stateNum == 3) {
			status = 5;
			tips = '确认把该老师设为离职吗？'
		}

		var that = this;
		wx.showModal({
			title: '提示',
			content: tips,
			success: function(e) {
				if(e.confirm) {
					app.request({
						url: 'school/changeTeacherStatus',
						data:{
							teacher_id: that.data.teacherId,
							status: status,
							school_id: that.data.schoolId
						},
						sCallback:(res) => {
							if(res.code == 1) {
								that.setData({
								 	stateNum: status
								 });
							}
							app.alert(res.msg)
						}
					})
					// var loginInfo = loginFn()
					// loginInfo.teacherId = that.data.alldetail.teacherId;
					// loginInfo.schoolId = that.data.schoolId;
					// loginInfo.isAdmin = 1;
					// loginInfo.status = status;

					// wx.request({
					// 	url: urls.saveTeacher,
					// 	method: 'POST',
					// 	data: loginInfo,
					// 	success: function(res) {
					// 		if (res.data.code == '0') {
					// 			 wx.showToast({
					// 			   title: '成功',
					// 			 })
					// 			 that.setData({
					// 			 	stateNum: status
					// 			 });
					// 		}
					// 	}
					// })
				}
			}
		});
		
	}
})
